package com.gjy.concurrency.thread;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-05-17 09:28:35
 */
public class ThreadPoolTest {

    private static final Logger logger = LoggerFactory.getLogger(ThreadPoolTest.class);

    public static void main(String[] args) throws InterruptedException {

        ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(3);

        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                2, 4, 0, TimeUnit.SECONDS,
                queue,
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );
        int n = 8;

        for (int i = 0; i < n; i++) {
            pool.submit(() -> {
                String name = Thread.currentThread().getName();
                logger.info("thread name = {}", name);
            });
        }

        pool.shutdown();
    }
}
